﻿function GetDay(obj) {
    var result = "";
    var num = obj.getDay();
    switch (num) {
        case 0:
            {
                result = "星期日";
            } break;
        case 1:
            {
                result = "星期一";
            } break;
        case 2:
            {
                result = "星期二";
            } break;
        case 3:
            {
                result = "星期三";
            } break;
        case 4:
            {
                result = "星期四";
            } break;
        case 5:
            {
                result = "星期五";
            } break;
        case 6:
            {
                result = "星期六";
            } break;
    }
    return result;
}

function GetMenuTypes() {
    var returnvalue = null;
    $.ajax({
        type: "GET",
        async: false,
        url: "/jsserver/all/menutype.aspx",
        dataType: "json",
        success: function (data) {
            returnvalue = data;
        },
        error: function () {
            returnvalue = null;
        }
    }
   );
    return returnvalue;
}

function GetMenuParentPages() {
    var returnvalue = null;
    $.ajax({
        type: "GET",
        async: false,
        url: "/jsserver/all/parentpage.aspx",
        dataType: "json",
        success: function (data) {
            returnvalue = data;
        },
        error: function () {
            returnvalue = null;
        }
    }
   );
    return returnvalue;
}

//id is div where contains tree
function GetChildPages(parentId) {
    var returnvalue = null;
    var date = new Date();
    $.ajax({
        type: "GET",
        async: false,
        url: "/jsserver/page/childs.aspx",
        data: { parentId: parentId, date: date.getMilliseconds() },
        dataType: "json",
        success: function (data) {
            returnvalue = data;
        },
        error: function () {
            returnvalue = null;
        }
    }
   );
    return returnvalue;
}

function DynamicBuildPageTreeView(divId) {
    var parentNodes = GetMenuParentPages();

    if (parentNodes != null) {
        for (i = 0; i < parentNodes.length; i++) {
            var parentNode = parentNodes[i];

            $("#" + divId).append("<div id='firstMenuDiv" + parentNode.Id +
        "' class='leftMenuTitle' style='cursor: pointer;'><a id='firstMenuA" + parentNode.Id +
        "' href='/ContentMangeAddChildMenu.aspx?nodeid=" + parentNode.Id + "'>" + parentNode.PageName + "</a></div>");

            $("#firstMenuDiv" + parentNode.Id).after("<ul id='firstMenuUl" + parentNode.Id + "' class='leftMenu'></ul>");

            var childNodes = GetChildPages(parentNode.Id);

            if (childNodes != null) {
                for (j = 0; j < childNodes.length; j++) {
                    var childNode = childNodes[j];
                    $("#firstMenuUl" + parentNode.Id).append("<li><a id='secondMenu" + childNode.Id + "' href='/ContentManageChildMenuEdit.aspx?nodeid=" + childNode.Id +
            "'>" + childNode.PageName + "</a></li>");
                }
            }
        }
    }
}

/************************************model manage js**************************************************/
function GetContentData(index, count, who) {
    var returnvalue = null;
    var date = new Date();
    $.ajax({
        type: "GET",
        async: false,
        url: "/jsserver/content/data.aspx",
        data: { who: who, index: index, count: count, date: date.getMilliseconds() },
        dataType: "json",
        success: function (data) {
            returnvalue = data;
        },
        error: function () {
            returnvalue = null;
        }
    }
   );
    return returnvalue;
}

function GetContentCount(who) {
    var returnvalue = null;
    var date = new Date();
    $.ajax({
        type: "GET",
        async: false,
        url: "/jsserver/content/count.aspx",
        data: { who: who, date: date.getMilliseconds() },
        dataType: "json",
        success: function (data) {
            returnvalue = data;
        },
        error: function () {
            returnvalue = null;
        }
    }
   );
    return returnvalue;
}

function deleteContent(who, id) {
    var date = new Date();
    $.ajax({
        type: "GET",
        async: false,
        url: "/jsserver/content/delete.aspx",
        data: { who: who, id: id, date: date.getMilliseconds() },
        success: function (data) {
            if (data == "True") {
                switch (who) {
                    case "LeaveMessage":
                        {
                            $("#newaddtr" + id).remove();
                        } break;
                    case "JoinUsMessage":
                        {
                            $("#joinnewaddtr" + id).remove();
                        } break;
                    case "FriendLink":
                        {
                            $("#friendnewaddtr" + id).remove();
                        } break;
                }
            }
        },
        error: function () {
            alert('删除失败！');
        }
    }
   );
}

function deleteListContent(tableId, checks) {
    for (i = 0; i < checks.length; i++) {
        var temp = $(checks[i]).val();
        if (temp > 0) {
            var who = "";
            switch (tableId) {
                case "leaveMessageTable":
                    {
                        who = "LeaveMessage";
                    } break;
                case "joinusMessageTable":
                    {
                        who = "JoinUsMessage";
                    } break;
                case "friendLinkTable":
                    {
                        who = "FriendLink";
                    } break;
            }
            var date = new Date();
            $.ajax({
                type: "GET",
                async: false,
                url: "/jsserver/content/delete.aspx",
                data: { who: who, id: temp, date: date.getMilliseconds() },
                success: function (data) {
                    if (data == "True") {
                        switch (who) {
                            case "LeaveMessage":
                                {
                                    $("#newaddtr" + temp).remove();
                                } break
                            case "JoinUsMessage":
                                {
                                    $("#joinnewaddtr" + temp).remove();
                                } break;
                            case "FriendLink":
                                {
                                    $("#friendnewaddtr" + temp).remove();
                                } break;
                        }
                    }
                },
                error: function () {
                }
            }
   );
        }
    }

}

function updateFriendLink(fid) {
    var ftitle = $("#friendtitle" + fid).val();
    var furl = $("#friendurl" + fid).val();
    $.ajax({
        type: "POST",
        async: false,
        url: "/jsserver/friendlink/update.aspx",
        data: { id: fid, title: ftitle, furl: furl },
        success: function (data) {

        },
        error: function () {
            alert('更新失败！');
        }
    }
   );
}

function showSeparateContentOnTable(index, count, who) {
    switch (who) {
        case "LeaveMessage":
            {
                var data = GetContentData(index, count, who);
                if (data != null) {
                    var trs = $("#leaveMessageTr").parent().children();
                    if (trs.length > 3) {
                        for (i = 0; i < trs.length - 3; i++) {
                            $(trs[i + 1]).remove();
                        }
                    }
                    for (j = 0; j < data.length; j++) {
                        $("#leaveMessageTr").after("<tr id='newaddtr" + data[j].Id +
                        "'><td><input type='checkbox' id='newaddcheck" + data[j].Id + "' value='" + data[j].Id +
                        "'/></td><td>" + data[j].UserName + "</td><td>" + data[j].LeaveTime +
                        "</td><td>" + data[j].UserPhone + "</td><td><label id='detailleavemessagebtn" + data[j].Id +
                        "' class='btn' onclick='alert(\"" + data[j].Content + "\")'>查看详细</label><label id=\"deleteleavemessagebtn" + data[j].Id +
                        "\" class='btn' onclick='deleteContent(\"LeaveMessage\",\"" + data[j].Id + "\");'>删除</label></td></tr>");
                    }
                }
            } break;
        case "JoinUsMessage":
            {
                var data = GetContentData(index, count, who);
                if (data != null) {
                    var trs = $("#joinusMessageTr").parent().children();
                    if (trs.length > 3) {
                        for (i = 0; i < trs.length - 3; i++) {
                            $(trs[i + 1]).remove();
                        }
                    }
                    for (j = 0; j < data.length; j++) {
                        $("#joinusMessageTr").after("<tr id='joinnewaddtr" + data[j].Id +
                        "'><td><input type='checkbox' id='joinnewaddcheck" + data[j].Id + "' value='" + data[j].Id +
                        "'/></td><td>" + data[j].UserName + "</td><td>" + data[j].UserMobilePhone +
                        "</td><td>" + data[j].UserStaticPhone + "</td><td>" + data[j].ContactTime + "</td><td><label id='detailjoinusbtn" + data[j].Id +
                        "' class='btn' onclick='alert(\"" + data[j].Content + "\")'>查看详细</label><label id=\"deletejoinusbtn" + data[j].Id +
                        "\" class='btn' onclick='deleteContent(\"JoinUsMessage\",\"" + data[j].Id + "\");'>删除</label></td></tr>");
                    }
                }
            } break;
        case "FriendLink":
            {
                var data = GetContentData(index, count, who);
                if (data != null) {
                    var trs = $("#friendLinkTr").parent().children();
                    if (trs.length > 3) {
                        for (i = 0; i < trs.length - 3; i++) {
                            $(trs[i + 1]).remove();
                        }
                    }
                    for (j = 0; j < data.length; j++) {
                        $("#friendLinkTr").after("<tr id='friendnewaddtr" + data[j].Id +
                        "'><td><input type='checkbox' id='friendnewaddcheck" + data[j].Id + "' value='" + data[j].Id +
                        "'/></td><td><input type='text' id='friendtitle" + data[j].Id + "' value='" + data[j].Title +
                        "'/></td><td><input type='text' id='friendurl" + data[j].Id + "' value='" + data[j].Url +
                        "'></td><td><label id='editfriendbtn" + data[j].Id +
                        "' class='btn' onclick=\"updateFriendLink("+data[j].Id+")\">更新</label><label id=\"deletefriendbtn" + data[j].Id +
                        "\" class='btn' onclick='deleteContent(\"FriendLink\",\"" + data[j].Id + "\");'>删除</label></td></tr>");
                    }
                }
            } break;
    }
}

function separateLabelClick(index, count, who) {
    $("#separatelabel" + (index + 1)).attr("style", "font-size:15px;color:Red;cursor:pointer;");
    showSeparateContentOnTable(index, count, who);    
}

//separatecount每页显示条数,controlId要添加进的控件id
function DynamicBuildSeparatePageLabel(separatecount, controlId, who) {
    var count = GetContentCount(who);
    if (count > 0 && separatecount > 0) {
        $("#" + controlId)[0].innerHTML = "";
        var pages = parseInt(count / separatecount);
        if (pages >= 0) {
            var pendStr = "";
            for (i = 1; i <= (pages + 1); i++) {
                var tempStr = "";
                //                pendStr = "<label id='separatelabel" + i + "' style='cursor:pointer;text-decoration:underline' onclick='showSeparateContentOnTable("
                //                + (i - 1) + "," + separatecount + ",'" + who + "'); $(this).attr('style','font-size:15px;color:Red');'>" + i +
                //                "</label>";
                tempStr = "<label id='separatelabel" + i + "' style='cursor:pointer;text-decoration:underline' onclick=\"separateLabelClick("
                + (i - 1) + "," + separatecount +
                ", '"+who+"')\">" + i + "</label>";
                $("#" + controlId).append(tempStr);
            }
            //            $("#" + controlId)[0].innerHTML = pendStr;
        }
        showSeparateContentOnTable(0, 10, who);
    }
}

/***************************************xhedit up js************************************/
/******************************************************************************/